package org.cagiobox.contactfinder;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public class TestMain {

	static Logger log = Logger.getLogger(TestMain.class.getName());

	public static void main(String[] args) {

		String outFilePath = "c:/temp/EmailList.txt";

		InputSequencer sequencer = new InputSequencer();

		List<String> f_urls = sequencer.getURLList("resources/f_links.xml");
		List<String> g_urls = sequencer.getURLList("resources/g_links.xml");

		List<String> f_email = new ArrayList<String>();
		List<String> g_email = new ArrayList<String>();

		String baseURL_Opti_de = "http://www.opti-mediaservices.de/prj_205/view/";

		HttpClient http = new HttpClient();
		Extractor extractor = new Extractor();

		String content = null;
		String email = null;

		for (String url : f_urls) {

			try {
				content = http.httpGet(baseURL_Opti_de + url);
				email = extractor.extractEmail(content);
				f_email.add(email);

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		for (String url : g_urls) {

			try {
				content = http.httpGet(baseURL_Opti_de + url);
				email = extractor.extractEmail(content);
				g_email.add(email);

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		// write file
		PrintWriter printWriter = null;
		try {
			// Will overwrite the file if exists or creates new
			printWriter = new PrintWriter(outFilePath, "UTF-8");

			printWriter.println("Frames");
			for (String emailaddr : f_email) {
				printWriter.println(emailaddr);
			}

			printWriter.println("SunGlasses");
			for (String emailaddr : g_email) {
				printWriter.println(emailaddr);
			}

		} catch (FileNotFoundException fileNotFoundException) {
			fileNotFoundException.printStackTrace();
		} catch (UnsupportedEncodingException unsupportedEncodingException) {
			unsupportedEncodingException.printStackTrace();
		} finally {
			printWriter.close();
		}

		System.out.println("Elaboration completed");
	}

}
